package com.in.universityinfo;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.in.universityinfo.adapter.SpinnerAdapter;
import com.in.universityinfo.constant.ConstantVariables;
import com.in.universityinfo.database.UniversityDatasource;

public class CitySelectionActivity extends BaseActivity {
	private ImageButton btnGo;
	private Spinner spnCity;
	private UniversityDatasource database;
	private List<String> listCities;
	private TextView txtSelectedInfo;
	private String strSelectedState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_city_selection);

		// get screen parameters
		strSelectedState = getIntent().getStringExtra(
				ConstantVariables.EXTRA_STATE);

		btnGo = (ImageButton) findViewById(R.id.btnGo);
		spnCity = (Spinner) findViewById(R.id.spnCity);

		// Set text
		txtSelectedInfo = (TextView) findViewById(R.id.txtSelectedInfo);
		String selectedInfo = "The cities in " + strSelectedState;
		txtSelectedInfo.setText(selectedInfo);

		btnGo.setOnClickListener(this);
		
		TextView _disclaimer = (TextView) findViewById(R.id.txtDisclaimer);
		_disclaimer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(CitySelectionActivity.this,
						DisclaimerActivity.class);
				startActivity(i);

			}
		});

		database = new UniversityDatasource(getApplicationContext());
		setupAdmob();
		share();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		database.open();
		initSpinner();
	}

	private void initSpinner() {

		listCities = database.getCities(strSelectedState);
		// listRechargePlan = database.getRechargePlans();

		SpinnerAdapter spnStatesAdapter = new SpinnerAdapter(this,
				android.R.layout.simple_spinner_item, listCities);
		spnStatesAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnCity.setAdapter(spnStatesAdapter);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		database.close();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.btnGo:
			if (null != spnCity.getSelectedItem()
					&& !spnCity.getSelectedItem().toString().isEmpty()) {
				Intent i = new Intent(CitySelectionActivity.this,
						UniversitySelectionActivity.class);
				i.putExtra(ConstantVariables.EXTRA_STATE, strSelectedState);
				i.putExtra(ConstantVariables.EXTRA_CITY, spnCity
						.getSelectedItem().toString());

				startActivity(i);
				break;
			}
		}
	}
}
